package com.theydream.pocketpets.ARcaught;


import java.util.Enumeration;
import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.theydream.pocketpets.ARKit.ARLayout;
import com.theydream.pocketpets.libs.FourSqareVenue;
import com.theydream.pocketpets.libs.FourSquareClient;

public class HoldMeUp extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	CustomCameraView cv;
	public static volatile Context ctx;
	ARLayout ar;
	volatile Location curLocation = null;

	private LocationListener gpsListener = new LocationListener() {

		public void onLocationChanged(Location location) {
			Log.e("HoldMe", "Got first");
			if (curLocation != null)
				return;
			curLocation = location;
			new Thread() {
				public void run() {
					FourSquareClient fc = new FourSquareClient();
					Vector<FourSqareVenue> vc = fc.getVenuList(curLocation);
					Log.e("Where4",
							"CurLocation LA:" + curLocation.getLatitude()
									+ " LO:" + curLocation.getLongitude());
					ar.clearARViews();
					if (vc != null && vc.size() > 0) {
						Enumeration e = vc.elements();
						while (e.hasMoreElements()) {
							FourSqareVenue fq = (FourSqareVenue) e
									.nextElement();
							Log.e("Where4", "Got Venue:" + fq.name);
							if (fq.location != null)
								Log.i("Where4",
										"Lat:" + fq.location.getLatitude()
												+ ":"
												+ fq.location.getLongitude());
							Log.e("Where4", "Azimuth: " + fq.azimuth);
							ar.addARView(fq);
						}
					}
				}
			}.start();
			LocationManager locMan = (LocationManager) ctx
					.getSystemService(Context.LOCATION_SERVICE);
			locMan.removeUpdates(this);
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

	};

	private void addLoadingLayouts() {
		FourSqareVenue fs = new FourSqareVenue(this.getApplicationContext());
		fs.azimuth = 90;
		fs.inclination = 0;
		fs.name = "a";
		ar.addARView(fs);
		// fs = new FourSqareVenue(this.getApplicationContext());
		// fs.azimuth = 45;
		// fs.inclination = 0;
		// fs.name = "����;
		// ar.addARView(fs);
		// fs = new FourSqareVenue(this.getApplicationContext());
		// fs.azimuth = 90;
		// fs.inclination = 0;
		// fs.name = "����;
		// ar.addARView(fs);
		// fs = new FourSqareVenue(this.getApplicationContext());
		// fs.azimuth = 135;
		// fs.inclination = 0;
		// fs.name = "����;
		// ar.addARView(fs);
		fs = new FourSqareVenue(this.getApplicationContext());
		fs.azimuth = 180;
		fs.inclination = 40;
		fs.name = "a";
		ar.addARView(fs);
		// fs = new FourSqareVenue(this.getApplicationContext());
		// fs.azimuth = 210;
		// fs.inclination = 0;
		// fs.name = "����;
		// ar.addARView(fs);
		// fs = new FourSqareVenue(this.getApplicationContext());
		// fs.azimuth = 270;
		// fs.inclination = 0;
		// fs.name = "����;
		// ar.addARView(fs);
		ar.postInvalidate();
	}

	public void onStart() {
		super.onStart();
	}

	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);

			ctx = this.getApplicationContext();
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			requestWindowFeature(Window.FEATURE_NO_TITLE);

			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);

			ar = new ARLayout(getApplicationContext());

			cv = new CustomCameraView(this.getApplicationContext());
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			// cl = new CompassListener(this.getApplicationContext());
			WindowManager w = getWindowManager();
			Display d = w.getDefaultDisplay();
			int width = d.getWidth();
			int height = d.getHeight();
			ar.screenHeight = height;
			ar.screenWidth = width;
			FrameLayout rl = new FrameLayout(getApplicationContext());
			rl.addView(cv, width, height);
			ar.debug = true;
			rl.addView(ar, width, height);

			// ARLayout temp =new ARLayout(this);

			ar.setOnClickListener(this);

			setContentView(rl);
			LocationManager locMan = (LocationManager) ctx
					.getSystemService(Context.LOCATION_SERVICE);
			locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 1,
					gpsListener);
			// Log.e("Where","Orientation:"+i);
			// rl.bringChildToFront(cl);
			addLoadingLayouts();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onDestroy() {
		super.onDestroy();
		cv.closeCamera();
		ar.close();
		// cl.close();	
		// cv.closeCamera();
	}

	// Catch view
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// Toast.makeText(this, "Don't touch me.", Toast.LENGTH_SHORT).show();
		if (ar.direction<190 && ar.direction>170) {
			
			cv.closeCamera();
			Intent toDialog = new Intent(HoldMeUp.this, CamaraView.class);
			startActivity(toDialog);
			//Intent toDialog1 = new Intent (HoldMeUp.this, CamaraView.class);
			//startActivity(toDialog1);
		}
		if (ar.direction<100 && ar.direction>80) {
			Intent toDialog = new Intent(HoldMeUp.this, Caught.class);
			startActivity(toDialog);
			//Intent toDialog1 = new Intent (HoldMeUp.this, CamaraView.class);
			//startActivity(toDialog1);
		}
	}
}